package com.etown.util.email;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import sun.misc.BASE64Encoder;

public class MailMessage {
	private Session session;

	private String maiSubject;

	private StringBuffer content;

	private MimeMessage message;

	private String[] mailTo = null;

	private String[] mailCc = null;

	private boolean isSuccess = true;

	private String mailFrom;

	private MimeMultipart mm = new MimeMultipart("related");

	private LinkedList IMGList = new LinkedList();

	public boolean isSuccess() {
		return this.isSuccess;
	}

	public MailMessage() {
		this.message = new MimeMessage(this.session);
	}

	MailMessage(Session paramSession) throws MessagingException {
		this.session = paramSession;
		this.message = new MimeMessage(paramSession);
		this.message.setSentDate(new Date());
	}

	MailMessage(Session paramSession, String paramString)
			throws MessagingException {
		this.session = paramSession;
		this.message = new MimeMessage(paramSession);
		this.message.setSentDate(new Date());
		this.message.setSender(new InternetAddress(paramString));
	}

	MailMessage(Session paramSession, String paramString1, String paramString2)
			throws MessagingException {
		this.session = paramSession;
		this.message = new MimeMessage(paramSession);
		this.message.setSentDate(new Date());
		this.message.setSender(new InternetAddress(paramString1));
		this.message.setSubject(paramString2);
	}

	public void setMaiSubject(String paramString) throws MessagingException {
		try {
			this.maiSubject = paramString;
			BASE64Encoder localBASE64Encoder = new BASE64Encoder();
			this.message.setSubject("=?GBK?B?"
					+ localBASE64Encoder.encode(paramString.getBytes()) + "?=");
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public void setMailFrom(String paramString) throws MessagingException {
		try {
			this.mailFrom=paramString;
			this.message.setSender(new InternetAddress(paramString));
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public void setMailTo(String paramString) throws MessagingException {
		setMailTo(new String[] { paramString });
	}

	public void setMailTo(String[] paramArrayOfString)
			throws MessagingException {
		this.mailTo = checkMail(paramArrayOfString);
		if (this.mailTo == null) {
			this.isSuccess = false;
			return;
		}
		InternetAddress[] arrayOfInternetAddress = new InternetAddress[this.mailTo.length];
		try {
			for (int i = 0; i < this.mailTo.length; ++i)
				arrayOfInternetAddress[i] = new InternetAddress(this.mailTo[i]);
		} catch (AddressException localAddressException) {
			localAddressException.printStackTrace();
			this.isSuccess = false;
		}
		this.message.setRecipients(Message.RecipientType.TO,
				arrayOfInternetAddress);
	}

	public void setMailCc(String paramString) throws MessagingException {
		setMailCc(new String[] { paramString });
	}

	public void setMailCc(String[] paramArrayOfString)
			throws MessagingException {
		this.mailCc = checkMail(paramArrayOfString);
		if (this.mailCc == null)
			return;
		InternetAddress[] arrayOfInternetAddress = new InternetAddress[this.mailCc.length];
		try {
			for (int i = 0; i < this.mailCc.length; ++i)
				arrayOfInternetAddress[i] = new InternetAddress(this.mailCc[i]);
		} catch (AddressException localAddressException) {
			localAddressException.printStackTrace();
			this.isSuccess = false;
		}
		this.message.setRecipients(Message.RecipientType.CC,
				arrayOfInternetAddress);
	}

	public void setContent(String paramString) throws MessagingException {
		setContent(paramString, "text/html;charset=GBK", false);
	}

	public void setContent(String paramString1, String paramString2,
			boolean paramBoolean) throws MessagingException {
		String str = paramString2;
		if (paramString2 == null) {
			str = "text/html;charset=GBK";
		}

		MimeBodyPart localMimeBodyPart = new MimeBodyPart();

		localMimeBodyPart.setContent(paramString1, str);
		this.mm.setSubType("related");
		this.mm.addBodyPart(localMimeBodyPart);

		if (paramBoolean)
			for (int i = 0; i < this.IMGList.size(); ++i) {
				localMimeBodyPart = new MimeBodyPart();
				FileDataSource localFileDataSource = new FileDataSource(
						this.IMGList.get(i).toString());

				localMimeBodyPart.setDataHandler(new DataHandler(
						localFileDataSource));

				localMimeBodyPart.setFileName(this.IMGList.get(i).toString());
				localMimeBodyPart.setHeader("Content-ID", this.IMGList.get(i)
						.toString());

				this.mm.addBodyPart(localMimeBodyPart);
			}
	}

	public void setAttach(String paramString) throws MessagingException {
		LinkedList localLinkedList = new LinkedList();
		localLinkedList.add(paramString);
		setAttach(localLinkedList);
	}

	public void setAttach(List paramList) throws MessagingException {
		if (paramList == null)
			return;
		for (int i = 0; i < paramList.size(); ++i) {
			MimeBodyPart localMimeBodyPart = new MimeBodyPart();
			localMimeBodyPart = new MimeBodyPart();
			FileDataSource localFileDataSource = new FileDataSource(paramList
					.get(i).toString());
			localMimeBodyPart.setDataHandler(new DataHandler(
					localFileDataSource));

			localMimeBodyPart.setFileName(localFileDataSource.getName());

			this.mm.addBodyPart(localMimeBodyPart);
		}
	}

	public void saveChanges() throws MessagingException {
		this.message.setContent(this.mm);
		this.message.saveChanges();
	}

	private String[] checkMail(String[] paramArrayOfString) {
		String[] arrayOfString1 = null;
		if (paramArrayOfString == null)
			return null;
		String[] arrayOfString2 = new String[paramArrayOfString.length];
		int i = 0;
		for (int j = 0; j < paramArrayOfString.length; ++j) {
			if ((paramArrayOfString[j] == null)
					|| (paramArrayOfString[j].indexOf("@") <= 0)
					|| (paramArrayOfString[j].split("@").length != 2)) {
				++i;
			} else
				arrayOfString2[(j - i)] = paramArrayOfString[j];
		}
		arrayOfString1 = new String[paramArrayOfString.length - i];
		for (int j = 0; j < arrayOfString1.length; ++j) {
			arrayOfString1[j] = arrayOfString2[j];
		}
		return arrayOfString1;
	}

	public MimeMessage getMessage() {
		return this.message;
	}

	public Session getSession() {
		return this.session;
	}

	public void setSession(Session paramSession) {
		this.session = paramSession;
	}

	public StringBuffer getContent() {
		return this.content;
	}

	public void setContent(StringBuffer paramStringBuffer) {
		this.content = paramStringBuffer;
	}

	public LinkedList getIMGList() {
		return this.IMGList;
	}

	public void setIMGList(LinkedList paramLinkedList) {
		this.IMGList = paramLinkedList;
	}

	public String getMaiSubject() {
		return this.maiSubject;
	}

	public String[] getMailTo() {
		return this.mailTo;
	}

	public String[] getMailCc() {
		return this.mailCc;
	}

	public String getMailFrom() {
		return this.mailFrom;
	}

	public MimeMultipart getMm() {
		return this.mm;
	}

	public void setMessage(MimeMessage paramMimeMessage) {
		this.message = paramMimeMessage;
	}

	public void setSuccess(boolean paramBoolean) {
		this.isSuccess = paramBoolean;
	}
}